Skip to content

perf(security): reject obfuscated scans with no targeted gap - #2

Closed
rufushsu9987 wants to merge 1147 commits into
mainfrom
cursor/unicode-security-view-perf-02fc
Closed

rufushsu9987 wants to merge 1147 commits into
mainfrom
cursor/unicode-security-view-perf-02fc

Conversation

@rufushsu9987

Copy link
Copy Markdown
Owner

Fixes NVIDIA#866.

The Unicode security view was spending almost all of its time in per-character Python loops inside the obfuscated-instruction automaton (_fold_security_character, _security_skeleton_piece, _has_security_word_character_before), including on ordinary prose that cannot contain a targeted vocabulary gap. This skips that automaton when a linear projection shows no such gap, and keeps the exact scan for everything else.

This does not overlap with NVIDIA#583. That change seeks token gaps, narrows the confusable class, and caches normalized views. It does not change _obfuscated_instruction_matches. No other open PR references NVIDIA#866.

What changed

  • ASCII skeletons are a 128-entry table built from the existing NFKC / confusable / casefold predicate (including dotted capital I). Non-ASCII skeletons are cached per character (lru_cache(8192)), not per document.
  • Before the automaton runs, text is projected onto letters, line breaks, and dots. A possessive per-word pattern reports a candidate only when a fixed vocabulary word contains a targeted gap. Contiguous phrases such as ignore previous instructions are not candidates.
  • Confusables, ligatures, and any non-ASCII character that can spell a vocabulary letter still take the exact automaton. Existing-projection gaps (zero-width, bidi, default ignorables, Hangul fillers) are deleted in the projection because they are not targeted gaps by themselves. A false positive on the reject only falls through to the exact scan.
  • The exact scan consumes action-start matches from one finditer instead of a fresh re.search at every idle cursor. Each consumption, including the final miss, still checks the runtime once. A reject checks once up front, once per 4096 characters, and once after the scan, so a long plain input stays cancellable.
  • Letter-spacing and security_reconstruction.py are unchanged. On accented input, security_text_views is still dominated by the letter-spacing walk when a runtime check is installed.

Benchmark

uv run python tests/benchmarks/bench_unicode_security_view.py on a 431,000-character paragraph (the input size from NVIDIA#866). Best of 5, Linux, Python 3.12. The "exact automaton" column is the previous scanner.

Case Exact automaton Fast reject
ASCII prose, matcher only 263.8 ms (1,595 KB/s) 11.1 ms (37,756 KB/s)
Accented prose, matcher only 315.6 ms (1,333 KB/s) 41.0 ms (10,271 KB/s)
Obfuscated tail, matcher only 247.8 ms (1,699 KB/s) 251.9 ms (1,671 KB/s)
security_text_views + runtime, ASCII 312.0 ms (1,349 KB/s) 61.9 ms (6,799 KB/s)
security_text_views + runtime, accented 892.5 ms (472 KB/s) 595.1 ms (707 KB/s)

An obfuscated tail does not take the reject, so that pair stays on the exact automaton. The issue's ~20 KB/s figure is whole-program time across many windows; this table is one 431 KB pass.

Test plan

  • Equivalence tests pin the public scanner against the exact automaton on confusables, zero-width, bidi, combining marks, Hangul fillers, ligatures, astral characters, 1,500 randomized strings (seed 866), and the repo's fixture/example texts.
  • Existing obfuscated-instruction and deadline tests, including the digit-gap timeout on the 4th runtime check.
  • uv run pytest -q (non-integration, non-provider): 14,779 passed, 20 skipped, 250 deselected, 4 xfailed.
  • ruff check, ruff format --check, and mypy on src/skillspector/artifacts.py and the new tests.

AI assistance: Cursor Grok 4.7 assisted with investigation, implementation, regression tests, and review.

Open in Web Open in Cursor 

rng1995 and others added 30 commits October 9, 2026 13:24
… semantics

Address the fourth review round.

- A forwarded **kwargs (a function's own ** parameter passed on) binds
  only into the callee's own ** parameter: one slot per callee, and it
  never fills named, keyword-only or receiver parameters. This removes
  the per-name expansion (up to 85 targets per **k) and the false
  positives from one tainted keyword reaching every forwarded one.
- A source call's value is what it reads, not its arguments, so
  json.load(open(os.getenv(...))) is TT4 again, as on main.
- Sinks cite variables main's flows taint (phase A) before parameters
  only the new flows bind, so same-rule messages keep main's wording.
- A class-body read of a name the class binds reads a view fed by the
  module and class keys; the class attribute keeps only the class's own
  value. Any class-body binding (annotated assignment, import, def)
  shadows a base's value; an annotation alone binds nothing.
- An immediately called lambda in a default yields its body's value.
- C3 merges every base and caps only the result.
- Deadline checks per argument and keyword in bind_arguments and every
  1,024 flows of one key in drain.
- CHANGELOG: exact fingerprints are version-bound; list every message
  change category and raised rule.

Tests pin each case, add non-root nested-default and many-base scaling
shapes, the forwarded-kwargs cost, the slot-cap boundary for *rest, the
multi-base ancestor cap, C3 local precedence, and non-constructor
self()/cls() calls.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bring in the main merges the update-branch workflow added to the PR
branch (NVIDIA#791, NVIDIA#792) while the fixes were in progress.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the conflict in static_patterns_tool_misuse.py: keep this
branch's _is_assigned_value next to main's new _bounded_context and
_raw_classification_bounds helpers, and use main's line_number()
in the TM3 loop after the assigned-umask skip.

Signed-off-by: Ilai Goldschmidt <117302862+ilaigold@users.noreply.github.com>
Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>
…-paths

fix(report): preserve archive member provenance in JSON
class D(Base, Mixin) with Base(Root) must resolve Root.handle before
Mixin.handle, as CPython's MRO does. Add that shape and a twin whose
Root.handle is harmless to the C3 test. Merging the bases list first,
or merging the bases in reverse, now fails the test.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Flag a base64, base32, xxd or openssl decoder whose output is piped to a
shell or a stdin-reading interpreter, passed to eval, sh -c or python -c,
or fed to a shell or source through process substitution or a
here-string. These rules run on every file type, so SKILL.md and hook
configs are covered.

Decode literal base64, base32 or hex payloads given to these decoders and
re-apply SC2, as the XOR helper does. Variables, files and downloads stay
opaque.

Signed-off-by: Raj Zalavadia <rzalavad@redhat.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
)

* fix(batch): preserve malformed response failures in compatibility mode

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* fix(batch): preserve analyzer deadlines through compatibility initialization

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* fix(meta): reject invalid stringified response fields

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* test(meta): import result schema for validation regressions

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* test(meta): enable parametrized invalid response coverage

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* Update meta response validation expectations

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* Format malformed response regressions

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* fix: preserve valid compat verdicts and guard timeout forwarding

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* fix: respect shorter compatibility request deadlines

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* fix: bound pooled request waiting and retries by one deadline

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

* style: format compatibility regression checks

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>

---------

Signed-off-by: yashrajbasav <yashrajbasav@nvidia.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot and others added 29 commits October 16, 2026 18:55
Conflict came from main's TM3 umask fix (94547bd/3eb3cd93, PR NVIDIA#783), which
added the _is_assigned_value skip to the TM3 match loop that this branch
instruments with runtime_check(). Kept both: runtime_check() runs first,
then main's assigned-umask skip.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since NVIDIA#828, the shipped-bytecode walk counts only its own CPU time against
the 5-second budget, and the workflow wall-clock deadline bounds elapsed
time. Word the resource-bounds row the same way as the YARA rule-load row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
…failures

fix: report incomplete batch gap-fill analysis
…ope-analysis

fix(analyzer): reuse tool-misuse context indexes and honor deadlines
…ut-race

fix: prevent report output symlink races
docs(bounds): describe the SC8 budget as active processing time
…boundaries

Keep main's module-level _BIDI_CONTROL_PATTERN block (NVIDIA#473) next to the
P2 identifier-boundary constants; the P2_PATTERNS list merges without
the bidi entry.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Two review blockers on NVIDIA#452:

- `<!-->` and `<!--->` are abrupt empty comments; the HTML tokenizer
  closes them immediately. A P2 match no longer starts there, so the
  visible text after them is not reported as hidden up to a later `-->`.
- `<!-- PLEASEIGNOREPREVIOUSINSTRUCTIONS -->` has no separator or case
  transition, so identifier boundaries dropped a hidden instruction that
  main flags. `ignore`, `instruction(s)` and `transmit` rarely occur
  inside ordinary words, so they match as substrings again. `system`,
  `POST`, `GET` and `send` keep identifier boundaries, which is what
  keeps SYSTEMATIC, POSTGRES, GETTING and target from matching.

Direct and graph-level regressions cover both forms, including a real
hidden comment after an abrupt empty comment.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
…ent-boundaries

Fix P2 HTML comment boundary matching
…istry-option-validation

Reject unsupported skill options in registry mode
…-hardware-reset-state

fix(mp3): recognize descriptive hardware reset states
…nment-final-state

fix(tm1): use the final repeated assignment target
…ursive-shipped-baseline

fix(cli): reject shipped baselines in recursive multi-skill scans
…ursive-risk-recommendation

fix(report): align recursive risk recommendations
The Unicode security view spent most of its time in per-character
Python loops inside the obfuscated-instruction automaton, even on
prose that cannot contain a targeted vocabulary gap. Fold ASCII
skeletons through a 128-entry table, cache non-ASCII skeletons by
character, and skip the automaton when a linear projection cannot
spell a gapped vocabulary word.

Detection is unchanged: the reject is a superset of texts the exact
automaton can accept, and a miss still runs that automaton. Runtime
checks stay bounded on the reject path.

Signed-off-by: HSU Yu Chen <rufushsu9987@users.noreply.github.com>

Co-authored-by: Rufus Hsu <rufushsu9987@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: per-character Python loops in the Unicode security view dominate analysis (~20 KB/s)

8 participants